package com.mxpio.erp.equipment.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.mxpio.erp.common.equipment.entity.EquipmentArea;
import com.mxpio.erp.common.equipment.service.EquipmentAreaService;
import com.mxpio.erp.equipment.utils.QRCodeUtil;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "AreaController", description = "区域结构设置")
@RestController
@RequestMapping("/erp/equipment/area/")
public class AreaController {

    @Autowired
    private EquipmentAreaService equipmentAreaService;


    @GetMapping("list")
    @Operation(summary = "区域结构设置列表", description = "获取区域结构设置列表（无分页）", method = "GET")
    public Result<List<EquipmentArea>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentArea> list = equipmentAreaService.list(EquipmentArea.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("tree")
    @Operation(summary = "区域树", description = "获取区域树", method = "GET")
    public Result<List<EquipmentArea>> tree(Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentArea> equipmentAreas = equipmentAreaService.tree(EquipmentArea.class, criteria);
        return Result.OK(equipmentAreas);
    }

    @GetMapping("page")
    @Operation(summary = "区域结构设置列表", description = "获取区域结构设置列表（分页）", method = "GET")
    public Result<Page<EquipmentArea>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        //只查询父节点
        criteria = criteria.addCriterion("pid", Operator.EQ, "0");
        Page<EquipmentArea> pageResult = equipmentAreaService.listPage(EquipmentArea.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("childList/{pid}")
    @Operation(summary = "根据pid获取子节点区域结构设置列表", description = "根据pid获取子节点区域结构设置列表", method = "GET")
    public Result<List<EquipmentArea>> getChildList(@PathVariable(name = "pid", required = true) String pid) {
        Criteria c = Criteria.create().addCriterion("pid", Operator.EQ, pid);
        List<EquipmentArea> list = equipmentAreaService.list(EquipmentArea.class, c);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取区域结构设置列表", description = "根据ID获取区域结构设置列表", method = "GET")
    public Result<EquipmentArea> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentArea equipmentArea = equipmentAreaService.getById(EquipmentArea.class, id);
        return Result.OK(equipmentArea);
    }

    @RequestMapping("add")
    @Operation(summary = "保存区域结构设置", description = "保存区域结构设置")
    public Result<EquipmentArea> add(@RequestBody EquipmentArea equipmentArea) {
        try {
            equipmentAreaService.addArea(equipmentArea);
        }catch (MBootException e){
            return Result.error(e.getMessage());
        }
        return Result.OK(equipmentArea);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑区域结构设置", description = "编辑区域结构设置", method = "PUT")
    public Result<EquipmentArea> edit(@RequestBody EquipmentArea equipmentArea) {
        try {
            equipmentAreaService.updateArea(equipmentArea);
        }catch (MBootException e){
            return Result.error(e.getMessage());
        }

        return Result.OK(equipmentArea);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除区域结构设置", description = "删除区域结构设置,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentArea> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentAreaService.deleteArea(key);
        }
        return Result.OK();
    }

    @GetMapping("getImg")
    @Operation(summary = "设备基本信息-获取区域二维码", description = "设备基本信息-获取区域二维码", method = "GET")
    public Result<?> getImg(String id) {
        EquipmentArea area = equipmentAreaService.getById(EquipmentArea.class, id);
        JSONObject object = new JSONObject();
        object.put("id", id);
        object.put("areaName", area.getAreaName());
        object.put("areaCode", area.getAreaCode());
        String base64 = null;
        try {
            base64 = QRCodeUtil.getImageBase64(JSONUtil.toJsonStr(object));
        } catch (Exception e) {
            return Result.error("生成失败！");
        }

        return Result.OK(base64);
    }


}
